/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
var API = API || {};
        
API.apply = function(object, config, defaults) {
    if (defaults) 
        API.apply(object, defaults);            

    if (object && config && typeof config === 'object') {
        for (var i in config) {
            object[i] = config[i];
        }
    }
    return object;
};

(function(){

    requirejs.config({
        'baseUrl': 'API',
        'paths': {
            'app': '../app',
            
            //handlebars for template handling
            'handlebars': 'libs/handlebars/handlebars',
            
            //jQuery for DOM manipulation
            'jquery': 'libs/jquery/jquery',
            
            //undescore for aditional functionality
            'underscore': 'libs/underscore/underscore',
            
            //backbone for MVC and class extension
            'backbone': 'libs/backbone/backbone'
        },
        // Shim declaration
        'shim': {
            'underscore': {
              'exports': '_'
            },
            'handlebars': {
              'exports': 'Handlebars'
            },
            'backbone': {
              'deps': ['jquery', 'underscore'],
              'exports': 'Backbone'
            }
        }
    });

    define(['jquery','underscore', 'handlebars', 'backbone', 
        //These should be loaded later in the chain!!
        'model/BasicModel',
        'store/BasicStore',
        'API/grid/BasicGrid.js',
        'API/form/Option.js',
        'API/form/Selector.js',
        'API/form/Input.js',
        'API/form/BasicForm.js',
        'API/button/BasicButton.js',
        'API/toolbar/BasicToolbar.js',
        'API/window/BasicWindow.js',
        'API/viewport/BorderViewport.js'    
        // end these
    ], function($, _, handlebars, Backbone) {

        API.apply(API, {

            jquery : $,
            
            underscore : _,
            
            handlebars : handlebars,
            
            backbone : Backbone,

            isZero: function(nbr){
                return (nbr === 0);
            },

            appendItems: function(items, node){        
                for(var index in items){
                    node.appendChild(items[index].DOM);
                }
            },
            
            // For non ES5 enviroments
            create: function(prototype, properties){
                function c(){};
                this.apply(c.prototype, prototype, properties);
                return new c();                
            },
            
            /*
             * Why have 2 for each functions? 
             * Less efficient, easier to maintain. the code is more readable.
             * Most loops shouldn't be that long anyway
             */
            
            //Forward version of forEach
            forEachF: function(array,callback){
                for( var i=0, len = array.length; i<len; i++ )
                    callback(array[i],i,array);
            },
            
            //Backwards version of forEach. A bit more efficient. Use when possible.
            forEachB: function(array,callback){
                for( var i=array.length; i--; )
                    callback(array[i],i,array);
            }

        }); 
        
    });
}());